package com.freesky.biz.activity.controller;

import com.freesky.biz.activity.domain.TContract;
import com.freesky.biz.activity.domain.TStockMonitor;
import com.freesky.biz.activity.service.IContractService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController {

    @Autowired
    private IContractService contractService;

    /**
     * 获取合同项目列表
     */
    //@PreAuthorize("@ss.hasPermi('contract:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(TContract entity) {
        startPage();
        List<TContract> list = contractService.findList(entity);
        return getDataTable(list);
    }

    @GetMapping("/summary")
    public AjaxResult summary(TContract entity) {
        return AjaxResult.success(contractService.selectSummaryData(entity));
    }

    /**
     * 根据ID获取详细信息
     */
    //@PreAuthorize("@ss.hasPermi('contract:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable String configId) {
        TContract contract = contractService.get(configId);
        return success(contract);
    }

    /**
     * 新增合同管理
     */
    @PreAuthorize("@ss.hasPermi('contract:config:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TContract entity) {
        entity.setContractId(SeqUtil.getSeqId());
        entity.setCreateBy(String.valueOf(getUserId()));
        if(!contractService.checkContractNameUnique(entity)){
            return error("新增合同'" + entity.getContractName() + "'失败，合同名称已存在");
        }

        if(!contractService.checkContractNoUnique(entity)){
            return error("新增合同'" + entity.getContractName() + "'失败，合同编号已存在");
        }

        return toAjax(contractService.insert(entity));
    }

    /**
     * 修改合同管理
     */
    @PreAuthorize("@ss.hasPermi('contract:config:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TContract entity) {
        entity.setUpdateBy(String.valueOf(getUserId()));
        if(!contractService.checkContractNameUnique(entity)){
            return error("修改合同'" + entity.getContractName() + "'失败，合同名称已存在");
        }

        if(!contractService.checkContractNoUnique(entity)){
            return error("修改合同'" + entity.getContractName() + "'失败，合同编号已存在");
        }
        return toAjax(contractService.update(entity));
    }

    /**
     * 删除合同管理
     */
    @PreAuthorize("@ss.hasPermi('contract:config:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        for(String configId : configIds){
            int nCount = contractService.selectProjectCount(configId);
            if(nCount > 0){
                return AjaxResult.error("暂无法删除，请先删除关联的项目!");
            }
        }
        contractService.deleteByIds(configIds);
        return success();
    }
}
